.\" 
.\" pseudo(1) man page
.\" 
.\" Copyright (c) 2010 Wind River Systems, Inc.
.\"
.\" This program is free software; you can redistribute it and/or modify
.\" it under the terms of the Lesser GNU General Public License version 2.1 as
.\" published by the Free Software Foundation.
.\"
.\" This program is distributed in the hope that it will be useful,
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
.\" See the Lesser GNU General Public License for more details.
.\"
.\" You should have received a copy of the Lesser GNU General Public License
.\" version 2.1 along with this program; if not, write to the Free Software
.\" Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
.TH pseudo 1 "pseudo - pretending to be root"
.SH NAME
pseudo \- run a command in a virtual root environment
.SH SYNOPSIS
.B pseudo
.RB [ \-dflv ]
[
.B \-x
.I flags
]
[
.B \-P
.I prefix
]
[
.B \-rR
.I root
]
[
.B \-t
.I timeout
]
.RI [ command ]
.PP
.B pseudo \-h
.PP
.B pseudo
.RB [ \-dflv ]
[
.B \-x
.I flags
]
[
.B \-P
.I prefix
]
.RB [ \-BC ]
.BR \-i\  path
.PP
.B pseudo
.RB [ \-dflv ]
[
.B \-x
.I flags
]
[
.B \-P
.I prefix
]
.RB [ \-BC ]
.BR \-m\  from\  \-M\  to
.PP
.B pseudo
.RB [ \-dflv ]
[
.B \-x
.I flags
]
[
.B \-P
.I prefix
]
.B \-S
.PP
.B pseudo
.RB [ \-dflv ]
[
.B \-x
.I flags
]
[
.B \-P
.I prefix
]
.B \-V
.SH DESCRIPTION
The
.I pseudo
utility provides a virtual root environment, hereafter referred to as the
.IR pseudo\ environment ,
allowing the creation of file system images and packages by users
without root privileges.  The pseudo environment is implemented by pushing
a special library
.RI ( libpseudo.so )
into the
.B LD_PRELOAD
environment variable.  This library intercepts a large number of common
filesystem operations and some user-id related operations, and returns
values that look as though the operations had been performed by a root
user.  This is in turn managed by a daemon program which keeps a list
of virtualized file ownership and permissions; this daemon program itself
is
.IR pseudo .

The
.I pseudo
program itself can also be used as a program launcher.  The launcher
is used to automatically configure a working environment, then execute
processes within that environment.  Alternatively, you can bypass this
by setting up certain environment variables (see the
.B ENVIRONMENT
section below).  The
.I pseudo
client library
.RI ( libpseudo.so )
can then start the server automatically.

The
.I pseudo
command can be invoked in one of several possible modes:

.TP 8
.B \-B
The
.B \-B
option causes
.I pseudo
to scan its database, as with the
.B \-C
option, but instead of reporting mismatches,
.I pseudo
attempts to repair them.  Specifically, device and inode number mismatches
are corrected, and symlink or directory mismatches result in deletion of
database entries.
.TP 8
.B \-C
The
.B \-C
option causes
.I pseudo
to scan its database, comparing against the filesystem, and reporting likely
errors.  This may be unreliable when the server is actively running.
.TP 8
.B \-h
The
.B \-h
option causes
.I pseudo
to print a usage message and exit.
.TP 8
.B \-i
The
.B \-i
option causes
.I pseudo
to attempt to correct device number mismatches by
checking inodes; if
.I path
has the same inode number as recorded in the database, but a different
device number, all instances of the device number recorded in the database
are updated to the device number in the live filesystem for
.IR path .
This is intended to handle the mismatches that can occur when remounting
an NFS filesystem.  The
.B \-i
option implies the
.B \-C
option.  You can also specify the
.B \-B
option to request that the database be rebuilt.
.TP 8
.B \-m
The
.B \-m
and
.B \-M
options cause
.I pseudo
to rename files, replacing the string
.I from
with the string
.I to.
The
.B \-m
option pair implies the
.B \-C
option.  You can also specify the
.B \-B
option to request that the database be rebuilt.
.TP 8
.B \-V
The
.B \-V
option causes
.I pseudo
to print configuration information and exit immediately.
.TP 8
.B \-S
The
.B \-S
option causes
.I pseudo
to try to find an existing server, and if it finds one, instructs that
server to shut down as soon as all clients are detached from it.  Note
that the server will not shut down while clients are connected to it;
in this case,
.I pseudo
will print a list of the remaining client PIDs.
.TP 8
.B \-d
The
.B \-d
option causes pseudo to immediately detach and run in the background
as a daemon.  This is rarely useful except for debugging.
.PP
Finally, invoked without any of these options,
.I pseudo
sets up an emulated root environment, then invokes
.I command
if it was provided, otherwise a shell (using the
.B SHELL
environment variable if it is set, or
.I /bin/sh
otherwise).

The following options modify the behavior of
.IR pseudo :

.TP 8
.BI \-d\  (daemonize)
Run as a daemon;
.I pseudo
detaches from the calling environment and runs as a daemon.  The command
returns successfully if this appears to have succeeded, otherwise it
produces an error message and returns a failure status.

.TP 8
.BI \-f\  (foreground)
Run in the foreground;
.I pseudo
runs as a server, and does not try to start other commands.  This mode
is useful for debugging.

.TP 8
.BI \-l\  (log)
Enable logging.  The
.I pseudo
daemon will log every filesystem transaction in the log database.

.TP 8
.BI \-r\  root
.TP 8
.BI \-R\  root
Set the
.B PSEUDO_CHROOT
environment variable, running as though the program had called
.I chroot(2)
on the specified path.  With
.BR \-r ,
this implies changing the working directory to the specified directory;
with
.BR \-R ,
it does not.

.TP 8
.B \-t timeout
Set the timeout of the
.I pseudo
daemon, in seconds.  The default is currently 30 seconds.  After this
long with no attached clients, the
.I pseudo
daemon shuts down automatically.  The server never shuts down while it
has attached clients.  Note that this does not prevent continued use;
new clients can restart the daemon if they need it.

.TP 8
.BI \-v\  (verbose)
Increase the verbosity of the
.I pseudo
daemon, and the client library for any programs started by this
invocation of
.IR pseudo .
This is equivalent to the numeric form of the
.B PSEUDO_DEBUG
environment variable; multiple
.B \-v
options increase the debugging level.

.TP 8
.BI \-x\  (debug)
Set specific deugging flags (the
.I pseudo
utility's help message lists them). This is equivalent to the string
form of the
.B PSEUDO_DEBUG
environment variable.

.SH EXAMPLES
The two most common usages of
.I pseudo
are using it to run specific commands, and setting up an environment manually
for running various other commands.

For the first case, the usage is reasonably simple:

.sp
$
.I /path/to/pseudo
.br
# 
.I commands which require root privileges

You may have to use the
.BI \-P prefix
option to tell
.I pseudo
where to look for its database and server.  If you specify a full path,
.I pseudo
assumes that
.B PSEUDO_PREFIX
should be the path to the directory containing the
.I pseudo
program, or to the
.I /bin
directory containing the
.I pseudo
program.

The other way to use
.I pseudo
is by setting up an environment.  This is suitable for use in
.I Makefiles
or similar environments, where you want to run a series of commands in
the
.I pseudo
environment, but not to keep invoking the
.I pseudo
command.  To do this, set up the
.BR PSEUDO_PREFIX ,\  LD_PRELOAD ,\ and\ LD_LIBRARY_PATH
environment variables, then run programs normally.  You do not need to
separately invoke the
.I pseudo
daemon; the client library starts it as needed.

If you have moved a directory which
.I pseudo
was tracking, you may be able to get the database reattached using the
.B \-m
option.  A typical usage might be:

.sp
$
.I /path/to/pseudo
.B \-B \-m
.I oldpath
.B \-M
.I newpath
.br

This requests that
.I pseudo
replace the string
.I oldpath
with the string
.I newpath
at the beginnings of filenames, then regenerate the database, correcting any
device/inode numbers.

.SH DIAGNOSTICS
Depending on invocation, diagnostic messages usually go either to standard
error or to the file
.B PSEUDO_PREFIX
.IR /var/pseudo/pseudo.log .
By default,
.I pseudo
daemon messages go into the log file, but messages generated by the client
code go to standard error.  These can be changed using the
.B PSEUDO_DEBUG_FILE
environment variable, documented in
.BR ENVIRONMENT .
At the default logging level, only critical
messages are displayed.  If you have raised the logging level (using the
.I \-v
option or the
.B PSEUDO_DEBUG
environment variable), additional messages are displayed.  Levels higher
than 2 are very unlikely to be useful outside of
.I pseudo
development.

Diagnostic messages seen by default are those which are believed to indicate
either a serious internal flaw in
.I pseudo
or a completely unexpected failure from the underlying operating system.  In
normal use, you should see no diagnostic messages.

.SH ENVIRONMENT
The most significant environment variables for
.I pseudo
are
.B LD_PRELOAD
and
.BR LD_LIBRARY_PATH .
However, these variables have no special meaning to
.IR pseudo ;
rather, they are used in the standard way to manipulate the dynamic linker
into loading the
.I libpseudo
library so that it can intercept calls into the underlying C library.

The following environment variables are used directly by
.IR pseudo :

.TP 8
.B PSEUDO_BINDIR
This directory holds the path to the
.I pseudo
binary; by default, it is the
.I bin
directory under
.B PSEUDO_PREFIX.
.TP 8
.B PSEUDO_CHROOT
This variable holds the current emulated
.I chroot(2)
path.  Paths that are relative to this are treated as though they were
instead relative to the filesystem root.
.TP 8
.B PSEUDO_DEBUG
This variable holds either a numeric "debug level" for
.I pseudo
to run at, or a set of specific debugging flags, generally letters.
Use
.B pseudo -h
to see the available flags. In general, this is useful only for debugging
.I pseudo
itself.
.TP 8
.B PSEUDO_DEBUG_FILE
The name of a file to use for debugging messages from the pseudo client;
the default is to log to standard error.  If the string contains a single
.BR %s ,
that string is replaced with the short program name, and if it contains
a single
.BR %d ,
that string is replaced with the process ID.  Other format specifiers
(other than '%%') are not allowed.  By default, the
.I pseudo
server logs to the file
.I pseudo.log
in the
.I var/pseudo
directory, while clients log to standard error.
.TP 8
.B PSEUDO_DISABLED
If this variable is set to a value that doesn't look like f, F, n, N, s, S, or
a numeric zero, the
.I pseudo
client library does not modify the behavior of called functions, though it
continues to intercept them and block signals while processing them.  This
variable is reevaluated on every call to
.IR fork(2) ,\  clone(2)
or related functions. If the value starts with a lowercase or uppercase
.I s
, the pseudo client disables all server spawning and communications, but still
operates locally. This means that no filesystem mode or permissions changes
are actually recorded or reported, but functions like
.I chown()
will still report success, even though nothing happens. This function is
intended for debugging of issues which are complicated by the server's
involvement.
.TP 8
.B PSEUDO_ALLOW_FSYNC
If this variable is set, pseudo will allow
.I fsync()
and related system calls, even it was configured with the
.I --enable-force-async
option. Otherwise, that option results in all such calls being
discarded silently, even when
.B PSEUDO_DISABLED
is set. The value specified doesn't matter.
.TP 8
.B PSEUDO_ENOSYS_ABORT
If this variable is set, the
.I pseudo
client library calls
.I abort()
rather than setting
.I errno
to
.B ENOSYS
in the event of a call to a missing underlying function.  This variable has
no function outside of debugging
.I pseudo
itself.
.TP 8
.B PSEUDO_LIBDIR
This directory holds the path to the
.I pseudo
shared libraries; by default, it is the
.I lib
directory under
.BR PSEUDO_PREFIX .
(On 64-bit hosts,
.I lib64
is also used.)
.TP 8
.B PSEUDO_LOCALSTATEDIR
This directory holds the
.I pseudo
database files and log files; by default, it is the
.I var/pseudo
directory under
.BR PSEUDO_PREFIX .
.TP 8
.B PSEUDO_NOSYMLINKEXP
By default, when chrooted,
.I pseudo
prepends the chroot directory to
the paths used for absolute symlinks; this behavior ensures that
opening symlinks produces expected results in most cases.  In some
cases you may want to suppress this.  If this variable is unset, or
set to any value other than 0,
.I pseudo
expands symlink paths like this.  If this variable is set to 0,
the behavior is disabled.
.TP 8
.BR PSEUDO_OPTS
This variable holds options to be passed to any new
.I pseudo
servers started.  Typically, when
.I pseudo
is used as a launcher, this will be set automatically; however, you
can also use it to pass options when using
.B LD_PRELOAD
to manually run things in the
.I pseudo
environment.
.TP 8
.B PSEUDO_PASSWD
This variable holds the path to a directory containing password and
group files to use for emulation of various password and group routines.
It should be the path to a directory containing the
.I etc
directory containing files named
.I passwd
and
.IR group .
When
.I pseudo
is emulating a
.I chroot
environment, the chroot directory is used by preference.  The
parallelism between these cases is why this variable points at
the parent directory of
.I etc
rather than the directory containing the files. If there is no
.I chroot
environment, and this variable is also unset,
.I pseudo
falls back to a directory specified at configure time, with the
default being the root directory. This is controlled by the
.B PSEUDO_PASSWD_FALLBACK
definition.
.TP 8
.B PSEUDO_PREFIX
If set, the variable
.B PSEUDO_PREFIX
is used to determine the path to use to find the
.I pseudo
server, in
.BR PSEUDO_PREFIX /bin,
and the
.I pseudo
data files, in
.BR PSEUDO_PREFIX /var/pseudo.
This variable is automatically set by the
.I pseudo
program when it is used as a launcher.
.TP 8
.B PSEUDO_PROFILE_PATH
If
.I pseudo
was configured with profiling enabled, specifies a path in which to
write client profiling information for use with the
.I pseudo_profile
utility (not built by default).
.TP 8
.B PSEUDO_TAG
If this variable is set in a client's environment, its value is
communicated to the server at the beginning of each client session,
and recorded in the log database if any logging occurs related to a
specific client.  Note that different clients may have different tags
associated with them; the tag value is per-client, not per-server.
.TP 8
.BR PSEUDO_UIDS ,\  PSEUDO_GIDS
These variables are used internally to pass information about the current
emulated user and group identity from one process to another.
.TP 8
.B PSEUDO_UNLOAD
This variable is reevaluated on every call to
.IR fork(2) ,\ exec(3)
or related functions.  If the variable exists
.RI libpseudo.so
will be removed from
.B LD_PRELOAD
and 
.B PSEUDO_DISABLED 
behavior will also be triggered.  For processes
that simply 
.IR fork(2),
the behavior will be the same as if 
.B PSEUDO_DISABLED
was set.  For new processes, after a call to
.IR exec(3)\ or\ system(3)
pseudo will not be loaded in the new process.
.TP 8
.B SHELL
If set, this will be used when
.I pseudo
is invoked without either a command or one of the options which directs
it to do something other than run a command.  Otherwise,
.I pseudo
defaults to
.I /bin/sh .
.B
.SH BUGS
The
.I pseudo
database is not particularly robust in the face of whole directory trees
being moved, or changes in the underlying device and inode numbers.  It
has a reasonable chance of recovering if only the path or the device numbers
have changed, but it is not particularly designed to address this.  A future
release is expected to have improved resilience in these cases.

The filesystem on which
.I pseudo
keeps its database and files must at a minimum support UNIX domain sockets
and reasonable file locking semantics.  Note that
.I pseudo
relies on
.I flock(2)
locking semantics; a lock has to persist into a child process.  This should
probably eventually be fixed.

The
.I pseudo
client library is probably thread-safe, but has not been adequately tested
or debugged in that context.

Filesystem performance is noticably worse under
.I pseudo
than it is otherwise.  This is probably because nearly every operation
(other than reads and writes) involves at least one round-trip network
communication with the server, and probably some kind of database
activity.

.SH SEE ALSO
fakeroot(1), ld.so(8), pseudolog(1), sqlite3(1)
.SH FURTHER READING
Documentation of the internals of
.I pseudo
may be found in the
.I doc
subdirectory of the pseudo source tree.
